package com.wheat.vipcard.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;
import com.opensymphony.xwork2.ActionSupport;
import com.wheat.vipcard.bean.Business;
import com.wheat.vipcard.bean.BusinessDAO;

public class BusinessAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private Business business;

	private Map<String, Object> dataMap;

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	// ���ؽ���ͻ���
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Business getBusiness() {
		return business;
	}

	public void setBusiness(Business business) {
		this.business = business;
	}

	/**
	 * ����̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveBusiness() throws Exception {
		Transaction tr = null;
		System.out.println("business =" + business);
		try {
			if (business != null) {
				BusinessDAO dao = new BusinessDAO();
				tr = dao.getSession().beginTransaction();
				dao.save(business);
				tr.commit();
				dao.getSession().close();
			}
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		}
		return "saveBusiness";
	}

	/**
	 * �������е��̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllBusiness() throws Exception {
		try {
			BusinessDAO businessDAO = new BusinessDAO();
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", businessDAO.findAll());
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("result :" + result);
			return SUCCESS;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ���ص����̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getBusinessByType() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String bsType = request.getParameter("bsType");
			System.out.println("bsType =" + bsType);
			BusinessDAO businessDAO = new BusinessDAO();
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", businessDAO.findByBsType(Integer.parseInt(bsType)));
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("result :" + result);
			return SUCCESS;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ���ص����̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getBusinessById() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String disId = request.getParameter("disId");
			System.out.println("disId =" + disId);
			BusinessDAO businessDAO = new BusinessDAO();
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", businessDAO.findById(Integer.parseInt(disId)));
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("result :" + result);
			return SUCCESS;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	public String listBuzByPage() {
		request = ServletActionContext.getRequest();
		try {

		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

}
